package dk.aiszone.lang.sql;

import java.util.ArrayList;
import java.util.List;

import dk.aiszone.lang.sql.interfaces.SQLObject;

/**
 * <code>SQLObject</code> representing a SQL table.
 * 
 * @author ads
 */
public class SQLTable extends AbstractSQLObject implements SQLObject {

    private final String alias;
    private final List<SQLField> fields = new ArrayList<SQLField>();
    private final String table;

    /**
     * Constructor for the <code>SQLTable</code> object.
     * 
     * @param table
     *            The name of the table.
     */
    public SQLTable(String table) {
        this(table, null);
    }

    /**
     * Constructor for the <code>SQLTable</code> object.
     * 
     * @param table
     *            The name of the table.
     * @param alias
     *            <b>Optionally</b> The alias of the table.
     */
    public SQLTable(String table, String alias) {
        this.table = table;
        this.alias = alias;
    }

    @Override
    public SQLResult prepareSQL(SQLResult sql) {
        sql.append(table);
        if (alias != null) {
            sql.append(" AS ").append(alias);
        }
        return sql;
    }

    /* pp */void addField(SQLField field) {
        field.setParent(this);
        fields.add(field);
    }

    /**
     * Retrieve the alias of the <code>SQLTable</code>.
     * 
     * @return The alias of the <code>SQLTable</code> - or <code>null</code> is unset.
     */
    /* pp */String getAlias() {
        return alias;
    }

    /* pp */List<SQLField> getFields() {
        return fields;
    }

    /**
     * Retrieve the name of the table contained in <code>SQLTable</code>.
     * 
     * @return The name of the table.
     */
    /* pp */String getTableName() {
        return table;
    }
}